//
//  THTimeChooseView.m
//  TaiHaoCloud
//
//  Created by LeeSongtao on 16/6/13.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxTimeChooseView.h"
#import "SPTimeValue.h"

@interface SampuxTimeChooseView ()<THChooseTimeCellSeletedDelegate>
{
    NSMutableArray *_chooseTimeCellArr;
    UIButton *_lastSeletedBtn;
}


@end

@implementation SampuxTimeChooseView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = THSystemClearColor;
        _chooseTimeCellArr = [NSMutableArray array];
        [self setupSubViews];
    }
    return  self;
}

- (void)setupSubViews
{
    NSArray *arr =@[@"morning_x",@"noon_x",@"after_x",@"night_x"];
    for (int i = 0; i < 4; i ++)
    {
        SampuxTimeChooseCell *timeCell = [[SampuxTimeChooseCell alloc]initWithFrame:CGRectMake(0, i * self.height/4, self.width, self.height/4 )];
        [self addSubview:timeCell];
        timeCell.backgroundColor = THSystemClearColor;
        timeCell.cellType = i;
        timeCell.cellDelegate = self;
//        timeCell.timeLabel.text = [self getTimeTextFromBack:timeCell.cellType];
//        timeCell.speedLabel.text = [self getSpeedTextFromBack:timeCell.cellType];

        [timeCell.timeImg setImage:imageNamed(arr[i]) forState:UIControlStateNormal];
        [_chooseTimeCellArr addObject:timeCell];
        timeCell.seletedBtn.tag = i;
//        [UIView createLineViewInbottom:timeCell];

    }
}

#pragma mark THTimeChooseCellSeletedDelegate
- (void)clickTHChooseTimeCellSeleted:(UIButton *)btn
{
    self.selectedCellType = btn.tag;
}

- (void)setSelectedCellType:(THChooseTimeCellType)selectedCellType
{
    _selectedCellType = selectedCellType;
    
    [_chooseTimeCellArr enumerateObjectsUsingBlock:^(SampuxTimeChooseCell *obj, NSUInteger idx, BOOL * _Nonnull stop)
    {
        if (idx == selectedCellType)
        {
            self.selectedCell = obj;
            [obj.seletedBtn setImage:imageNamed(@"state_yes") forState:UIControlStateNormal];
            
        } else
        {
            [obj.seletedBtn setImage:imageNamed(@"circle") forState:UIControlStateNormal];

        }
    }];
    
    if (self.cellSelectedBlock) {
        self.cellSelectedBlock();
    }
}


//- (NSString *)getTimeTextFromBack:(THChooseTimeCellType)type
//{
//    NSArray *timeArr = @[@"12:00",@"04:45",@"20:33",@"14:54"];
//    return timeArr[type];
//}
//
//- (NSString *)getSpeedTextFromBack:(THChooseTimeCellType)type
//{
//     NSArray *speedArr = @[@"4 X",@"8 X",@"12 X",@"16 X"];
//    return speedArr[type];
//
//    
//}

- (void)setInitialModel:(SampuxInstallTimeModel *)initialModel
{
    _initialModel = initialModel;
    for (int i = 0 ; i < _chooseTimeCellArr.count ; i ++) {
        SampuxTimeChooseCell *cell = _chooseTimeCellArr[i];
        if (cell.cellType == THChooseTimeCellType_Morning) {
            cell.timeLabel.text = [SPTimeValue setTimeValue:_initialModel.onePoint];
            cell.speedLabel.text = [NSString stringWithFormat:@"%@℃",_initialModel.oneValue];
        }else if (cell.cellType == THChooseTimeCellType_Noon)
        {
            cell.timeLabel.text = [SPTimeValue setTimeValue:_initialModel.twoPoint];
            cell.speedLabel.text =  [NSString stringWithFormat:@"%@℃",_initialModel.twoValue];
        }else if (cell.cellType == THChooseTimeCellType_After)
        {
            cell.timeLabel.text = [SPTimeValue setTimeValue:_initialModel.threePoint];
            cell.speedLabel.text = [NSString stringWithFormat:@"%@℃",_initialModel.threeValue];
        }else
        {
            cell.timeLabel.text = [SPTimeValue setTimeValue:_initialModel.fourPoint];
            cell.speedLabel.text = [NSString stringWithFormat:@"%@℃",_initialModel.fourValue];
        }
    }
}

@end
